import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_layout/bloc/index/index_bloc.dart';
import 'package:flutter_layout/page/home.dart';
import 'package:flutter_layout/page/my.dart';

class IndexPage extends StatelessWidget {
  const IndexPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(create: (context) => IndexBloc(), child: Index());
  }
}

class Index extends StatelessWidget {
  Index({super.key});

  // body
  final List<Widget> bodyList = [
    Home(),
    My(),
  ];

  // tablist
  final List<BottomNavigationBarItem> tabList = [
    BottomNavigationBarItem(label: 'Home', icon: Icon(Icons.add_ic_call)),
    BottomNavigationBarItem(label: 'My', icon: Icon(Icons.add_ic_call)),
  ];

  @override
  Widget build(BuildContext context) {
    final indexBloc = BlocProvider.of<IndexBloc>(context);

    return BlocBuilder<IndexBloc, IndexState>(builder: (context, state) {
      return Scaffold(
        appBar: AppBar(title: Text('Index')),
        body: bodyList[state.bodyIndex],
        bottomNavigationBar: BottomNavigationBar(
          items: tabList,
          currentIndex: state.bodyIndex,
          onTap: (index) => indexBloc.add(IndexChangeEvent(index)),
        ),
      );
    });
  }
}
